package main

import (
	"Gee/gee"
	"log"
	"net/http"
	"time"
)

func onlyForv2() gee.HandlerFunc {
	return func(c *gee.Context) {
		t := time.Now()
		c.Fail(500,"Internal Server Error")
		log.Printf("[%d] %s in %v for group v2",c.StatusCode,c.Req.RequestURI,time.Since(t))
	}
}

func main()  {
	r := gee.New()
	r.Use(gee.Logger())
	r.Use(gee.Recovery())
	v1 := r.Group("/v1")
	v1.Use(onlyForv2())
	v2 := r.Group("/v2")
	v2.GET("/", func(c *gee.Context) {
		c.JSON(http.StatusOK,gee.H{
			"message":"ok",
		})
	})
	v1.GET("/", func(c *gee.Context) {
		c.JSON(http.StatusOK,gee.H{
			"ping":"pang",
		})
	})
	r.GET("/", func(c *gee.Context) {
		c.HTML(http.StatusOK,"<h1>Hello!</h1>")
	})
	r.GET("/hello", func(c *gee.Context) {
		c.String(http.StatusOK,"hello %s,you're at %s \n",c.Query("name"),c.Path)
	})
	r.POST("/login", func(c *gee.Context) {
		c.JSON(http.StatusOK,gee.H{
			"user":c.PostFrom("user"),
			"pwd":c.PostFrom("pwd"),
		})
	})

	r.GET("/panic", func(c *gee.Context) {
		names := []string{"geektutu"}
		c.String(http.StatusOK, names[100])
	})

	r.Run(":8888")
}
